package com.logictech.lims.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.Pagenation;

import com.logictech.lims.service.LogService;
import com.logictech.lims.service.SourcingService;
import com.logictech.lims.entity.Sourcing;
import com.logictech.activity.system.entity.SystemUser;
import com.logictech.activity.system.service.SystemUserService;
import com.logictech.lims.dao.SourcingDao;

@Service("sourcingService")
public class SourcingServiceImpl implements SourcingService {
	@Resource
	private SourcingDao sourcingDao;
	
	@Resource
	private LogService logService;
	
	@Resource
	private SystemUserService systemUserService;
	
	@Override
	public void doAdd(Sourcing sourcing) {
		sourcingDao.add(sourcing);
		
		Sourcing sourcingLog = sourcingDao.get(sourcing.getId());
		if(!StringUtils.isEmpty(sourcingLog.getConfirmUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getConfirmUser()));
			sourcingLog.setConfirmUserName(systemUser.getUserName());
		}
		if(!StringUtils.isEmpty(sourcingLog.getCreateUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getCreateUser()));
			sourcingLog.setCreateUserName(systemUser.getUserName());
		}
		logService.doAdd("1",sourcingLog);
	}

	@Override
	public void doEdit(Sourcing sourcing) {
		sourcingDao.update(sourcing);
		
		Sourcing sourcingLog = sourcingDao.get(sourcing.getId());
		if(!StringUtils.isEmpty(sourcingLog.getConfirmUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getConfirmUser()));
			sourcingLog.setConfirmUserName(systemUser.getUserName());
		}
		if(!StringUtils.isEmpty(sourcingLog.getCreateUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getCreateUser()));
			sourcingLog.setCreateUserName(systemUser.getUserName());
		}
		logService.doAdd("2",sourcingLog);
	}
	
	@Override
	public void doConfirm(Sourcing sourcing) {
		
		sourcingDao.update(sourcing);
		
		Sourcing sourcingLog = sourcingDao.get(sourcing.getId());
		if(!StringUtils.isEmpty(sourcingLog.getConfirmUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getConfirmUser()));
			sourcingLog.setConfirmUserName(systemUser.getUserName());
		}
		if(!StringUtils.isEmpty(sourcingLog.getCreateUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getCreateUser()));
			sourcingLog.setCreateUserName(systemUser.getUserName());
		}
		logService.doAdd("4",sourcingLog);
	}

	@Override
	public void doDelete(String id) {
		Sourcing sourcingLog = sourcingDao.get(id);
		if(!StringUtils.isEmpty(sourcingLog.getConfirmUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getConfirmUser()));
			sourcingLog.setConfirmUserName(systemUser.getUserName());
		}
		if(!StringUtils.isEmpty(sourcingLog.getCreateUser())){
			SystemUser systemUser = systemUserService.queryById(Long.parseLong(sourcingLog.getCreateUser()));
			sourcingLog.setCreateUserName(systemUser.getUserName());
		}
		logService.doAdd("3",sourcingLog);
		sourcingDao.delete(id);
	}

	@Override
	public Sourcing queryById(String id) {
		Sourcing sourcing  = sourcingDao.get(id);
		return sourcing;
	}

	@Override
	public PageList<Sourcing> queryPageList(
		PageQuery<Sourcing> pageQuery) {
		PageList<Sourcing> result = new PageList<Sourcing>();
		Integer itemCount = sourcingDao.count(pageQuery);
		List<Sourcing> list = sourcingDao.queryPageList(pageQuery,itemCount);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	@Override
	public List<Sourcing> queryList(PageQuery<Sourcing> pageQuery) {
		List<Sourcing> list = sourcingDao.queryList(pageQuery);
		return list;
	}
	
}
